package com.demo.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.demo.model.Canvas;
import com.demo.model.CanvasRes;
import com.demo.model.Resources;
import com.demo.service.CanvasResService;
import com.demo.service.CanvasService;
import com.demo.service.ResourcesService;

@Controller
@RequestMapping("/page")
public class PageController {
	
	@Autowired
	private CanvasService canvasService;
	@Autowired
	private CanvasResService canvasResService;
	@Autowired
	private ResourcesService resourcesService;
	
	@RequestMapping("/index")
	public String index(Model model,String tabId){
		List<Canvas> list = canvasService.findByOwner("1");
		model.addAttribute("canvas", list);
		model.addAttribute("tabId", tabId);
		return "index";
	}
	@RequestMapping("/getSwithTab")
	@ResponseBody
	public Map<String,Object> getSwithTab(String tabId){
		Map<String,Object> map = new HashMap<>();
		Canvas canvas = canvasService.get(tabId);
		map.put("canvas", canvas);
		List<CanvasRes> ress = canvasResService.findByCanvasId(tabId);
		map.put("ress", ress);
		return map;
	}
	@RequestMapping("/addLayout")
	public String addLayout(){
		return "addLayout";
	}
	@RequestMapping("/saveLayout")
	@ResponseBody
	public Canvas saveLayout(Canvas canvas){
		canvas = canvasService.saveLayout(canvas);
		return canvas;
	}
	@RequestMapping("/addLayoutRes")
	public String addLayoutRes(Model model){
		List<Resources> list = resourcesService.findAll();
		model.addAttribute("list", list);
		return "addLayoutRes";
	}
	@RequestMapping("/saveCanvasRes")
	@ResponseBody
	public Map<String,Object> saveCanvasRes(String json){
		Map<String,Object> map = new HashMap<>();
		map.put("code", "1");
		map.put("msg", "保存成功");
		try {
			canvasResService.saveByJson(json);
		} catch (Exception e) {
			map.put("code", "2");
			map.put("msg", "保存失败");
			e.printStackTrace();
		}
		return map;
	}
}
